{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 2 3 4 5 6]\n",
      "3\n",
      "6\n"
     ]
    }
   ],
   "source": [
    "import numpy as np\n",
    "array_1d = np.arange(1,7)\n",
    "print(array_1d)\n",
    "print(array_1d[2])\n",
    "print(array_2d[1,2])\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]]\n",
      "[4 5 6]\n"
     ]
    }
   ],
   "source": [
    "array_2d = np.arange(1,7).reshape(2,3)\n",
    "print(array_2d)\n",
    "print(array_2d[1])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    " 二维数组[行索引，列索引]、访问二维数组单个元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "————————————\n",
      "[[1 2 3]\n",
      " [4 5 6]]\n"
     ]
    }
   ],
   "source": [
    "array_22d= np.array([[1,2,3],[4,5,6],[7,8,9]])\n",
    "print(array_22d)\n",
    "print('————————————')\n",
    "print(array_22d[:2])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1]\n",
      " [4]]\n"
     ]
    }
   ],
   "source": [
    "print(array_22d[:2,0:1])# (访问(:2)前两行、(0:1)第一列的元素)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[1 4]\n"
     ]
    }
   ],
   "source": [
    "print(array_22d[:2,0])#(混合传入整数和切片并返回一维数组)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[2]\n",
      " [8]]\n"
     ]
    }
   ],
   "source": [
    "array_22d= np.array([[1,2,3],[4,5,6],[7,8,9],[10,11,12]])\n",
    "\n",
    "print(array_22d[::2,1::2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "#### 练习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  3  5  7]\n",
      " [ 2  4  6  8]\n",
      " [10 11 12 13]]\n",
      "-----------------\n",
      "[6 8]\n",
      "[[ 6  8]\n",
      " [12 13]]\n",
      "[[ 2  8]\n",
      " [10 13]]\n"
     ]
    }
   ],
   "source": [
    "arr33=np.array([[1,3,5,7],[2,4,6,8],[10,11,12,13]])\n",
    "print(arr33)\n",
    "print('-----------------')\n",
    "print(arr33[1,2:5])\n",
    "print(arr33[1:,2:])\n",
    "print(arr33[1:,(0,3)])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ True False  True]\n"
     ]
    }
   ],
   "source": [
    "mask = np.array([1,0,1],dtype=np.bool)\n",
    "print(mask)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": [
    "print(arr33[mask,2]) #利用布尔索引确定1 3行  然后第三列元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  3  5  7]\n",
      " [ 2  4  6  8]\n",
      " [10 11 12 13]]\n",
      "-----------------\n",
      "[2 4 6 8]\n",
      "-----------------\n",
      "[ 5  6 12]\n",
      "-----------------\n",
      "[[ 3  5]\n",
      " [ 4  6]\n",
      " [11 12]]\n",
      "-----------------\n",
      "[[ 6  8]\n",
      " [12 13]]\n",
      "-----------------\n",
      "[[ 3  7]\n",
      " [11 13]]\n"
     ]
    }
   ],
   "source": [
    "print(arr33)\n",
    "print('-----------------')\n",
    "print(arr33[1,:])\n",
    "print('-----------------')\n",
    "print(arr33[:,2])\n",
    "print('-----------------')\n",
    "print(arr33[:,1:3])\n",
    "print('-----------------')\n",
    "print(arr33[-2:,-2:])\n",
    "print('-----------------')\n",
    "print(arr33[::2,1::2])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "索引+1等于行"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 2.花式索引"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "[[1 2 3]\n",
      " [7 8 9]]\n"
     ]
    }
   ],
   "source": [
    "arr_222d=np.arange(1,10).reshape(3,3)\n",
    "print(arr_222d)\n",
    "print(arr_222d[[0,2]])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "二维数组[花式索引，花式索引]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[2 8]\n"
     ]
    }
   ],
   "source": [
    "print(arr_222d[[0,2],[1,1]])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 1  3  5  7]\n",
      " [ 2  4  6  8]\n",
      " [10 11 12 13]]\n",
      "-----------------\n",
      "[6 8]\n",
      "[[ 6  8]\n",
      " [12 13]]\n",
      "[ 5  8 12]\n",
      "[[ 2  8]\n",
      " [10 13]]\n"
     ]
    }
   ],
   "source": [
    "arr_33d=np.array([[1,3,5,7],[2,4,6,8],[10,11,12,13]])\n",
    "print(arr_33)\n",
    "print('-----------------')\n",
    "print(arr_33[1,2:5])\n",
    "print(arr_33[1:,2:])\n",
    "print(arr_33[[0,1,2],[2,3,2]])\n",
    "print(arr_33[1:,(0,3)])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 3.布尔索引"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "布尔索引指以布尔值组成的数组或列表为索引。当使用布尔索引访问数组时，会将布尔索引对应的数组或列表的元素作为索引，以获取索引为True时对应位置的元素"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[1 2 3]\n",
      " [4 5 6]\n",
      " [7 8 9]]\n",
      "[[False False False]\n",
      " [False False  True]\n",
      " [ True  True  True]]\n",
      "[6 7 8 9]\n"
     ]
    }
   ],
   "source": [
    "array_bool=np.arange(1,10).reshape(3,3)\n",
    "print(array_bool)\n",
    "print(array_bool>5)\n",
    "print(array_bool[array_bool>5])"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 4.reshape和resize函数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]\n"
     ]
    }
   ],
   "source": [
    "arr_shape=np.arange(16)\n",
    "print(arr_shape)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3  4  5  6  7]\n",
      " [ 8  9 10 11 12 13 14 15]]\n"
     ]
    }
   ],
   "source": [
    "print(arr_shape.reshape(2,8)) #reshape是生成一个副本"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]\n"
     ]
    }
   ],
   "source": [
    "print(arr_shape) #原数组不变"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {},
   "outputs": [
    {
     "ename": "ValueError",
     "evalue": "cannot reshape array of size 16 into shape (3,5)",
     "output_type": "error",
     "traceback": [
      "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m                                Traceback (most recent call last)",
      "\u001b[1;32m<ipython-input-42-7a484850ae67>\u001b[0m in \u001b[0;36m<module>\u001b[1;34m\u001b[0m\n\u001b[1;32m----> 1\u001b[1;33m \u001b[0mprint\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0marr_shape\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mreshape\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;36m3\u001b[0m\u001b[1;33m,\u001b[0m\u001b[1;36m5\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m",
      "\u001b[1;31mValueError\u001b[0m: cannot reshape array of size 16 into shape (3,5)"
     ]
    }
   ],
   "source": [
    "print(arr_shape.reshape(3,5))  #不能改变元素的总个数"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[ 0  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15]\n",
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]\n",
      " [12 13 14 15]]\n"
     ]
    }
   ],
   "source": [
    "arr_size=np.arange(16)\n",
    "print(arr_size)\n",
    "arr_size.resize(4,4)\n",
    "print(arr_size) #原地修改原数组"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[[ 0  1  2  3]\n",
      " [ 4  5  6  7]\n",
      " [ 8  9 10 11]]\n"
     ]
    }
   ],
   "source": [
    "arr_size.resize(3,4)\n",
    "print(arr_size)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.7.3"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
